/**
 * @Project RSLsXWG
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Apr 30, 2011
 * @Createtime 9:36:50 PM
 */

package com.xwg.msg
{
	import com.xwg.services.HGHTTPService;

	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class EmoticonSet extends EventDispatcher implements IEventDispatcher
	{
		private var _emoticonSet:Vector.<Emoticon>;

		private var _emoticonService:HGHTTPService;

		public function EmoticonSet(configs:String, target:IEventDispatcher = null)
		{
			super(target);

			_emoticonSet = new Vector.<Emoticon>();

			_emoticonService = new HGHTTPService(configs);
			_emoticonService.addEventListener(ResultEvent.RESULT, _resultHandler);
			_emoticonService.addEventListener(FaultEvent.FAULT, _faultHandler);
		}

		private function _faultHandler(event:FaultEvent):void
		{
			dispatchEvent(event);
		}

		private function _resultHandler(event:ResultEvent):void
		{
			/**
			 * using wild cart (*) to avoid warning!!!
			 */
			for each (var emo:* in event.result.hgRoot.emos.emo.source)
			{
				_emoticonSet.push(new Emoticon(emo.code, (event.result.hgRoot.global.prefixURL + emo.imagePath), emo.description));
			}

			dispatchEvent(event);
		}

		public function get emoticonSet():Vector.<Emoticon>
		{
			return _emoticonSet;
		}

		public function set emoticonSet(value:Vector.<Emoticon>):void
		{
			_emoticonSet = value;
		}

	}
}